package com.mall.user.services;

import com.mall.user.EmailService;
import com.mall.user.config.MailSourceProperties;
import com.mall.user.dto.UserRegisterRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;

/**
 * 创建日期: 2022-01-21-20-30
 * author : WQW
 * description:
 */

@Slf4j
@Service
public class EmailServiceImpl implements EmailService {


    @Autowired
    MailSourceProperties mailSourceProperties;

    @Autowired
    JavaMailSender javaMailSender;


    @Async("taskExecutor")
    @Override
    public void senderEmail(String uuid, UserRegisterRequest request) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject(mailSourceProperties.getSubject());
        simpleMailMessage.setFrom(mailSourceProperties.getFrom());
        simpleMailMessage.setTo(request.getEmail());
        StringBuilder text = new StringBuilder();
        text.append(mailSourceProperties.getUrl()).append("?uid=").append(uuid).append("&username=").append(request.getUserName());
        simpleMailMessage.setText(text.toString());
        javaMailSender.send(simpleMailMessage);
    }
}
